package com.kaka.bus.engine.user.register;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.kaka.bus.R;
import com.kaka.bus.api.RetrofitUtils;
import com.kaka.bus.base.RxBaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.constant.UserConstant;
import com.kaka.bus.engine.user.login.LoginActivity;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.util.ToastUtil;
import com.kaka.bus.util.logger.Logger;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/9/28.
 */

public class RegistPwdActivity extends RxBaseActivity {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_pwd_mm)
    EditText mEdtPwdMm;
    @BindView(R.id.edt_pwd_mmagain)
    EditText mEdtPwdMmagain;
    @BindView(R.id.btn_pwd_next)
    AppCompatButton mBtnPwdNext;
    @BindView(R.id.tv_pwd_gologin)
    TextView mTvPwdGologin;
    private String mPhone;
    private String verifycode;
    public static final int TAG_TOLOGIN = 1;
    public static final String PHONE = "phone";
    public static final String PWD = "pwd";
    public static final String TAG_TOLOGIN_REQUEST = "toLogin";
    public static final String INVITECODE = "invitecode";
    public static final String VERIFYCODE = "verifycode";

    public String mInviteCode;

    @Override
    protected void initView() {
        mToolbarName.setText(getString(R.string.regist));
        mToolbarBack.setOnClickListener(v -> finish());

        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_registpwd;
    }

    private void getData() {
        Intent intent = getIntent();
        if (intent != null) {
            mPhone = intent.getStringExtra(UserConstant.REGIST_PHONE);
//            mInviteCode = intent.getStringExtra(INVITECODE);
            verifycode = intent.getStringExtra(VERIFYCODE);

        }
    }

    @Override
    public void initListener() {
        RxView.clicks(mBtnPwdNext)
                .throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(v -> {
                    String pwd = mEdtPwdMm.getText().toString().trim();
                    String affirm_pwd = mEdtPwdMmagain.getText().toString().trim();

                    if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(affirm_pwd)) {
                        ToastUtil.showShort(this, getString(R.string.pwdnotnull));
                        return;
                    }

                    if (pwd.length() != 6 || affirm_pwd.length() != 6) {
                        ToastUtil.showShort(this, getString(R.string.veril));
                        return;
                    }

                    if (!pwd.equals(affirm_pwd)) {
                        ToastUtil.showShort(this, getString(R.string.pwd_notright));
                        return;
                    }
                    showDialog("注册中...");
                    /**
                     * 自动帮用户登录
                     */
                   addSubscription(RetrofitUtils.getInstance().build()
                           .regist(mPhone, pwd,verifycode)
                            .compose(T.D())
                            .subscribeWith(new Result<HttpWrapper<String>>() {
                                @Override
                                protected void onSuccess(HttpWrapper<String> bean) {
                                    if (bean.code.equals(Constant.SUCCESS_CODE)) {
                                         showMessage(bean.info);
                                        /**
                                         * 自动帮用户登录
                                         */
                                        toLogin(mPhone, pwd);
                                    } else if (bean.code.equals(Constant.SMS_TOKEN)) {
                                        showMessage(bean.info);
                                    }else{
                                        showMessage(bean.getInfo());
                                    }
                                }

                                @Override
                                protected void onFinish() {
                                    dissDialog();
                                }
                            }));
                });

        mTvPwdGologin.setOnClickListener(v -> {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        });
    }

    /**
     * 注册成功帮用户登录
     *
     * @param phone
     * @param pwd
     */
    private void toLogin(String phone, String pwd) {
        Logger.i("自动登录中");
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra(PHONE, phone);
        intent.putExtra(PWD, pwd);
        intent.putExtra(TAG_TOLOGIN_REQUEST, TAG_TOLOGIN);
        intent.setAction(TAG_TOLOGIN_REQUEST);
        startActivity(intent);
        finish();
    }
    @Override
    protected boolean needFullScreen() {
        return true;
    }
}
